﻿using Microsoft.AspNetCore.Mvc.Formatters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VM.Wechat
{
    public class wxHelper
    {
        /// <summary>
        /// 统一下单
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="orderNum"></param>
        /// <param name="orderTotal"></param>
        public static string  unifiedorder(string openId,string  orderNum,int  orderTotal) {

            ResEntity info = new ResEntity();
            //获取请求数据
            Dictionary<string, string> strParam = new Dictionary<string, string>();
            //小程序ID
            strParam.Add("appid", PayInfo.appid);
            //附加数据
            strParam.Add("attach", PayInfo.attach);
            //商品描述
            strParam.Add("body", PayInfo.body);
            //商户号
            strParam.Add("mch_id", PayInfo.mchid);
            //随机字符串
            strParam.Add("nonce_str", PayInfo.nonceStr);
            //通知地址 (异步接收微信支付结果通知的回调地址，通知url必须为外网可访问的url，不能携带参数。)
            strParam.Add("notify_url", PayInfo.notifyUrl);
            //用户标识
            strParam.Add("openid", openId);
            //商户订单号
            strParam.Add("out_trade_no", orderNum);
            //终端IP
            strParam.Add("spbill_create_ip", PayInfo.addrIp);
            //标价金额
            strParam.Add("total_fee", Convert.ToInt32(orderTotal).ToString());
            //交易类型
            strParam.Add("trade_type", PayInfo.tradeType);
            strParam.Add("sign", PayHelper.GetSignInfo(strParam, PayInfo.key));
            //获取预支付ID
            string preInfo = Utility.PostHttpResponse(PayInfo.orderUrl, PayHelper.CreateXmlParam(strParam));
            string strCode = PayHelper.GetXmlValue(preInfo, "return_code");
            string strMsg = PayHelper.GetXmlValue(preInfo, "return_msg");
            if (strCode == "SUCCESS")
            {
                //再次签名
                string nonecStr = PayInfo.nonceStr;
                string timeStamp = PayInfo.timeStamp;
                string package = "prepay_id=" + PayHelper.GetXmlValue(preInfo, "prepay_id");
                Dictionary<string, string> singInfo = new Dictionary<string, string>();
                singInfo.Add("appId", PayInfo.appid);
                singInfo.Add("nonceStr", nonecStr);
                singInfo.Add("package", package);
                singInfo.Add("signType", PayInfo.signType);
                singInfo.Add("timeStamp", timeStamp);
                //返回参数
                info.Msg = strMsg;
                info.Code = strCode;
                info.Id = orderNum;
                info.appId = PayInfo.appid;
                info.orderGuId = orderNum;
                info.Package = package;
                info.TimeStamp = timeStamp;
                info.NonceStr = nonecStr;
                info.SignType = PayInfo.signType;
                info.PaySign = PayHelper.GetSignInfo(singInfo, PayInfo.key);
                //listMsg.Add(info);
                //result = u.SuccessMsg(listMsg, "", "");
                WXApiDataResult<ResEntity> resend = new WXApiDataResult<ResEntity>() {
                    code=200,
                    msg=$@"成功:{strMsg}",
                    data=info 
                };

                return JsonConvert.SerializeObject( resend);
            }
            else
            {
                info.Code = strCode;
                info.Msg = strMsg;
                //listMsg.Add(info);
                //result = u.FailMsg(listMsg);
                WXApiDataResult<ResEntity> resend = new WXApiDataResult<ResEntity>()
                {
                    code = 403,
                    msg = $@"失败:{strMsg}",
                    data = null
                };
                return JsonConvert.SerializeObject(resend);
            }

     
        }


    }
}
